package ast;

import java.io.PrintStream;
import java.util.Scanner;
import java.util.Map;
import java.util.HashMap;
import java.util.Stack;

public class BlockStmt extends Stmt {
    public final UnitList block;
    protected Map<String, AbstractValue> localStateMap;

    public BlockStmt(UnitList b, Location loc) {
        super(loc);
        block = b;
        localStateMap = new HashMap<String, AbstractValue>();
    }

    public void print(PrintStream ps, String indent) {
        ps.print(indent + "{\n");
        block.print(ps, indent + "  ");
        ps.print(indent + "}");
    }

    public void print(PrintStream ps) {
        print(ps, "");
    }

    // TODO: scope management might have issues
    public void execute(Stack<Map<String, AbstractValue>> stateStack, Scanner scanner) throws Exception {
        // add a new scope to the stack
        stateStack.push(localStateMap);

        // intepret the block
        block.interpret(stateStack, scanner);

        // remove the scope from the stack
        stateStack.pop();
        return;
    }
}
  